unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,
  Hook, Messenger, jpeg, ExtCtrls, OleCtrls, SHDocVw, mshtml;

type
  TfrmMain = class(TForm)
    Memo: TMemo;
    cmdGetMemory: TButton;
    imgSplash: TImage;
    lblStatus: TLabel;
    lblStatusS: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure cmdGetMemoryClick(Sender: TObject);
    procedure webBrowserDocumentComplete(ASender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
  private
  public
    procedure Echo(targetString : string);
    procedure ShareMemory();
    procedure Inject();
  end;

var
  frmMain     : TfrmMain;

const
  targetExe : string = 'ideal.exe';
  hookGP    : string = '\yggy.dll';

implementation

{$R *.dfm}

procedure TfrmMain.cmdGetMemoryClick(Sender: TObject);
const
{$J+}
  Ready : Boolean = False;
{$J-}
begin
  if Ready then Echo(SharedMemory^.IPCMessage)
  else
  begin
    ShareMemory();
    Ready := True;
  end;
end;

procedure TfrmMain.Echo(targetString : string);
begin
  lblStatus.Caption := targetString;
  lblStatusS.Caption := targetString;
  Memo.Lines.Add(targetString);
  SendMessage(Memo.Handle, EM_SCROLLCARET, 0, 0);
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  Echo('Yggdrasil test console');
  Inject();
end;

procedure TfrmMain.ShareMemory();
begin
  if OpenMap() then Echo('Opened IPC memory for hook communication.')
  else Echo('Could not open IPC memory!');
end;

procedure TfrmMain.webBrowserDocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  iAll : IHTMLElement;
begin
  if webBrowser.LocationURL <> 'about:blank' then
  begin
    if Assigned(webBrowser.Document) then
    begin
      iAll := (webBrowser.Document AS IHTMLDocument2).body;
      {while iAll.parentElement <> nil do
      begin
        iAll := iAll.parentElement;
      end;
      ShowMessage(iAll.outerHTML);}
      ShowMessage(iAll.innerHTML);
      webBrowser.navigate('about:blank');
    end;
  end;
end;

procedure TfrmMain.Inject();
var
  Process           : DWord;
  StartInfo         : TStartupInfo;
  ProcInfo          : TProcessInformation;
  Path              : Array [0..MAX_PATH] of Char;
  Created, Injected : Boolean;

begin
  //Create process
  ZeroMemory(@StartInfo, SizeOf(TStartupInfo));
  StartInfo.cb := SizeOf(TStartupInfo);
  Created := CreateProcess(nil, PChar(targetExe), nil, nil, False, 0, nil, nil,
                           StartInfo, ProcInfo);
  if Created = true then Echo('Created ' + targetExe + '!')
  else Echo('Failed to create ' + targetExe + '!');

  //Injection
  Process := ProcInfo.hProcess;
  GetCurrentDirectory(MAX_PATH, Path);
  Injected := InjectLibrary(Process, PChar(Path + hookGP));
  if Injected = true then Echo('Injected '+ hookGP + '!')
  else Echo ('Failed to inject ' + hookGP + '!');
  
end;

end.